function handles = ampHistoAnalysis(handles)
% ampHistoAnalysis
%
% Editing:
% gs april 2005 -- private version for mapAnalysis2p0
% -----------------------------------------------------------------

% some initialization stuff
handles.data.map.mapStack.mapStackHistoThresh = handles.data.map.mapStack.mapStackHistoSyn;
[r,c,p] = size(handles.data.map.mapStack.mapStackHistoBase);

% INDIVIDUAL MAPS ***********************************************

% obtain map of direct responses to exclude them from the histograms
directMap = handles.data.map.mapStack.mapAvgMeanED;
directMap(~isnan(directMap)) = 0; 
directMap(isnan(directMap)) =  1;
directMap(directMap==0) = NaN;

% histo params
leftLim = -9;
binWidth = .5;
rightLim = 74;
ampHistoEdges = [-inf  (leftLim:binWidth:rightLim)  inf];
ampHistoEdges2 = ( leftLim-1 * binWidth : binWidth : rightLim + 1 * binWidth);

% for each baseline and synaptic map, obtain the histograms of the data
for n = 1 : p
    
    % histo of the baseline window data
    b = handles.data.map.mapStack.mapStackHistoBase(:,:,n);
    b = b.*directMap; % convert directs to NaNs
    c = reshape(b, 1, numel(b)); % vectorize
    baselineData(n, :) = c(~isnan(c)); % exclude NaNs
    baselineN(n, :) = histc(baselineData(n, :), ampHistoEdges);
    
    % use the baseline noise level to generate a thresholded binary map of responses
    baselineSD(n) = std(baselineData(n, :));
    d = handles.data.map.mapStack.mapStackHistoThresh(:,:,n) > 2*baselineSD(n); % 0 = null, 1 = synaptic response
    handles.data.map.mapStack.mapStackHistoThresh(:,:,n) = d.*directMap;        % NaN = direct response
    
    % histo of the synaptic window data 
    s = handles.data.map.mapStack.mapStackHistoSyn(:,:,n);
    s = s.*directMap;
    c = reshape(s, 1, numel(s));
    synapticData(n, :) = c(~isnan(c));
    synapticN(n, :) = histc(synapticData(n, :), ampHistoEdges);
end

% averaged individual histograms
baselineNIndMean = mean(baselineN);
synapticNIndMean = mean(synapticN);
baselineNIndSEM = std(baselineN)/sqrt(p);
synapticNIndSEM = std(synapticN)/sqrt(p);

% AVERAGED MAPS ***********************************************
% (already direct-corrected)
b = handles.data.map.mapStack.mapAvgHistoBase;
baselineDataMean = reshape(b, 1, numel(b));
baselineNMean = histc(baselineDataMean, ampHistoEdges);

s = handles.data.map.mapStack.mapAvgHistoSyn;
synapticDataMean = reshape(s, 1, numel(s));
synapticNMean = histc(synapticDataMean, ampHistoEdges);

% summed map across individual binarized/thresholded maps of responses
flagX = 0;
if flagX
    s=sum(handles.data.map.mapStack.mapStackHistoThresh, 3);
    s(isnan(s))=-inf;
    figure;
    set(gcf, 'Color', 'w');
    colormap(jet2);
    imagesc(s);
    set(gca, 'CLim', [-.1 p]);
    colorbar;
    set(gca, 'PlotBoxAspectRatio', [1 1 1]);
    set(gca, 'Visible', 'off');
end
% PLOTTING ***********************************************
% set up the figure
x = 100; y = 30; w = 1500; h = 1000;
hFigHist = figure('Position', [x y w h], 'Name', 'array2Dplot', ...
    'NumberTitle', 'off', 'Color', 'w', 'DoubleBuffer', 'on');
subplotRows = p+2; subplotCols = 1; plotnum = 0;

for n = 1:p
    % amplitude histograms, individual maps
    plotnum = plotnum+1;
    hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
    h = bar(ampHistoEdges2, baselineN(n, :), 'histc');
    set(h, 'FaceColor', 'none', 'EdgeColor', 'k', 'LineWidth', 1);
    hold on;
    h = bar(ampHistoEdges2, synapticN(n, :), 'histc');
    set(h, 'FaceColor', 'none', 'EdgeColor', 'r', 'LineWidth', 1);
    ylabel('Observations');
    legend('Baseline window', 'Synaptic window');
    legend boxoff;
    title(['map ' num2str(n)]);
end

% amplitude histograms, mean histo of all individual histos
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
h = bar(ampHistoEdges2, baselineNIndMean, 'histc');
% h = errorbar(ampHistoEdges2, baselineNIndMean, baselineNIndSEM);
set(h, 'FaceColor', 'none', 'EdgeColor', 'k', 'LineWidth', 1);
hold on;
h = bar(ampHistoEdges2, synapticNIndMean, 'histc');
% h = errorbar(ampHistoEdges2, synapticNIndMean, synapticNIndSEM);
set(h, 'FaceColor', 'none', 'EdgeColor', 'r', 'LineWidth', 1);
ylabel('Observations');
legend('Baseline window', 'Synaptic window');
legend boxoff;
title('mean histo based on all individual histo data');

% amplitude histograms, histo of mean map
plotnum = plotnum+1;
hsub(plotnum) = subplot(subplotRows,subplotCols,plotnum);
h = bar(ampHistoEdges2, baselineNMean, 'histc');
set(h, 'FaceColor', 'none', 'EdgeColor', 'k', 'LineWidth', 1);
hold on;
h = bar(ampHistoEdges2, synapticNMean, 'histc');
set(h, 'FaceColor', 'none', 'EdgeColor', 'r', 'LineWidth', 1);
xlabel('Amplitude (pA)');
ylabel('Observations');
legend('Baseline window', 'Synaptic window');
legend boxoff;
title('histo of mean data');

if ~get(handles.chkShowHistoAnalysis, 'Value');
    delete(hFigHist);
else
%     set(hFigHist, 'Visible', 'on');
end